import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wheel_of_fortune/model/address_model.dart';

const mainColor = Color(0xFF041D57);
const whiteColor = Color(0xFFFFFFFF);
const yellowColor = Color(0xFFFFCC00);
const deepYellowColor = Color(0xFFFFB32C);
const black3AColor = Color(0xFF3A3A3A);
const grey6EColor = Color(0xFF6E6E6E);
const grey70Color = Color(0xFF707070);
const greyDBColor = Color(0xFFDBDBDB);

class Utils {
  static double screenWidth = 0;
  static double screenHeight = 0;
  static Size screenSize = Size.zero;
  static setScreenWidth(w) {
    screenWidth = w;
  }

  static setScreenHeight(h) {
    screenHeight = h;
  }

  static setScreenSize(s) {
    screenSize = s;
  }
}

class DateTimeHelper {
  static String getSpinTime(
      String startDate, String endDate, String startTime, String endTime) {
    final start = DateTime.parse(
        '${startDate ?? '2020-08-08'} ${startTime ?? '09:00:00'}');
    final startMonth = DateTimeHelper.getEnMonth(start.month);
    final startDay = start.day;
    final startYear = start.year;
    final starttime = start.hour;
    final end =
        DateTime.parse('${endDate ?? '2020-09-08'} ${endTime ?? '10:00:00'}');
    final endMonth = DateTimeHelper.getEnMonth(end.month);
    final endDay = end.day;
    final endYear = end.year;
    final endtime = end.hour;
    return '$startMonth $startDay, $startYear - $endMonth $endDay, $endYear from $starttime ${DateTimeHelper.getAmOrPm(starttime)} to $endtime ${DateTimeHelper.getAmOrPm(endtime)}';
  }

  static String getEnMonth(int mon) {
    switch (mon) {
      case 1:
        return 'January';
        break;
      case 2:
        return 'February';
        break;
      case 3:
        return 'March';
        break;
      case 4:
        return 'April';
        break;
      case 5:
        return 'May';
        break;
      case 6:
        return 'June';
        break;
      case 7:
        return 'July';
        break;
      case 8:
        return 'August';
        break;
      case 9:
        return 'September';
        break;
      case 10:
        return 'October';
        break;
      case 11:
        return 'November';
        break;
      case 12:
        return 'December';
        break;
      default:
        return '';
        break;
    }
  }

  static String getAmOrPm(int hour) {
    if (hour < 13) {
      return 'AM';
    } else {
      return 'PM';
    }
  }

  static bool isCanSpin(currentTime, startTime, endTime, startDate, endDate) {
    final current = DateTime.parse(currentTime);
    final now = DateTime.now().toString().substring(0, 10);
    final todayStart = DateTime.parse('$now $startTime');
    final todayEnd = DateTime.parse('$now $endTime');
    final start = DateTime.parse('$startDate $startTime');
    final end = DateTime.parse('$endDate $endTime');

    if (current.isAfter(todayStart) &&
        current.isBefore(todayEnd) &&
        current.isAfter(start) &&
        current.isBefore(end)) {
      return true;
    } else {
      return false;
    }
  }

  static String getDate(String createdTime) {
    final dateTime = DateTime.parse(createdTime);
    return '${dateTime.day} ${DateTimeHelper.getEnMonth(dateTime.month)} ${dateTime.year} ${dateTime.hour}:${dateTime.minute} ${getAmOrPm(dateTime.hour)}';
  }
}

class AddressHelper {
  static Future<List<ProvinceModel>> getProvinceList(context,
      {String regionCode}) async {
    final provinceString = await DefaultAssetBundle.of(context)
        .loadString("assets/address/province-list.json");
    List data = json.decode(provinceString);
    List<ProvinceModel> list = [];
    if (regionCode != null) {
      data.forEach((item) {
        if (regionCode == item['region_code']) {
          list.add(ProvinceModel.fromJson(item));
        }
      });
    } else {
      data.forEach((item) {
        list.add(ProvinceModel.fromJson(item));
      });
    }

    return list;
  }

  static Future<List<CityModel>> getCityList(context,
      {String provinceCode}) async {
    final provinceString = await DefaultAssetBundle.of(context)
        .loadString("assets/address/city-municipality-list.json");
    List data = json.decode(provinceString);
    List<CityModel> list = [];
    if (provinceCode != null) {
      data.forEach((item) {
        if (provinceCode != null && item['province_code'] == provinceCode) {
          list.add(CityModel.fromJson(item));
        }
      });
    } else {
      data.forEach((item) {
        list.add(CityModel.fromJson(item));
      });
    }
    return list;
  }
}

showToast({msg = ''}){
  Fluttertoast.showToast(msg: msg, gravity: ToastGravity.CENTER);
}
